//
//  MyRedenvelopesStore.m
//  NewPro
//
//  Created by 何荧红 on 2019/6/24.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "MyRedenvelopesStore.h"
#import "TicketModel.h"
#import "MyRedenvelopesModel.h"
#import "UserShopTicketsModel.h"

@implementation MyRedenvelopesStore

//获取红包列表
- (void)GetDetailWithStatus:(NSString *)status AndSize:(NSString *)size AndCurrent:(NSString *)current Success:(void(^)(NSDictionary *res))success Failure:(void(^)(NSError *error))failure{
    NSString *url = [NSString stringWithFormat:@"%@/api/tickets/page",IP];
    NSDictionary *prams = @{
                            @"status":status,
                            @"size":size,
                            @"current":current
                            };
    [HttpTool getUrlWithString:url parameters:prams success:^(id responseObject) {
        NSError *erro = [HttpTool inspectError:responseObject];
        if (erro == nil) {
            success(responseObject);
        }else{
            failure(erro);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}


#pragma mark - 获取model可用的卡券
- (void)getModelUserRedBgWithModelId:(NSString *)modelId Success:(void(^)(NSArray *arr, NSInteger totalCount))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/ticket/model/%@",IP,modelId];
    
    [HttpTool getUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            NSArray *arr = [UserShopTicketsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"userShopTickets"]];
            
            NSInteger total = 0;
            for (UserShopTicketsModel *model in arr) {
                
                total += model.count;
            }
            success(arr,total);
            
        }else{
            failure(error);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

//根据卡券类型id获取用户的可用卡券
- (void)getTicketsUserRedBgWithTicketId:(NSString *)ticketId Success:(void(^)(NSArray *arr))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/tickets/enable/%@",IP,ticketId];
    NSDictionary *prams = @{
                            @"current":@"1",
                            @"size":@"999999"
                            };
    [HttpTool getUrlWithString:url parameters:prams success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            NSArray *arr = [MyRedenvelopesModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"records"]];
            success(arr);
            
        }else{
            failure(error);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}
@end
